package cn.Alibaba.org.controller;


import cn.Alibaba.basic.annotation.PreAuthorize;
import cn.Alibaba.basic.util.JsonResult;
import cn.Alibaba.basic.util.PageList;
import cn.Alibaba.org.domain.Department;
import cn.Alibaba.org.query.DepartmentQuery;
import cn.Alibaba.org.service.IDepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/department")
@Api(value = "部门的API",description="部门相关的CRUD功能")
@RestController
public class DepartmentController {

    @Autowired
    private IDepartmentService service;


    /**
     * 部门数
     * @return
     */
    @GetMapping
    @ApiOperation(value = "查询展示所有数据" )
    @PreAuthorize(name="部门列表",sn = "department:getAll")
    public List<Department> findAll(){
        return service.findAll();
    }

    @GetMapping("/deptTree")
    @ApiOperation(value = "查询部门数 无极限" )
    @PreAuthorize(name="部门树",sn = "deptTree")
    public List<Department> deptTree(){
        return service.deptTree();
    }

    /**
     * 查询单个
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询数据" )
    @PreAuthorize(name="查询单个对象",sn = "department:getOne")
    public Department findOne(@PathVariable("id") Long id){
        return service.findOne(id);


    }

    /**
     * 删除操作
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "通过ID删除数据" )
    @PreAuthorize(name="部门删除",sn = "department:delete")
    public JsonResult delete(@PathVariable("id") Long id){

        try {
            service.del(id);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败");
        }

    }

    @PatchMapping
    @ApiOperation(value = "批量删除" )
    @PreAuthorize(name="批量删除",sn = "department:PatchDel")
    public JsonResult PatchDel(@RequestBody List<Long> ids){

        try {
            service.PatchDel( ids);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败");
        }

    }

    /**
     * 添加修改
     * @param department
     * @return
     */
    @PutMapping
    @ApiOperation(value = "通过ID添加或者修改数据" )
    @PreAuthorize(name="部门保存",sn = "department:save")
    public JsonResult addOrUpdate(@RequestBody  Department department ){
        try {
            if (department.getId()==null){
                service.save(department);
            }else {
                service.update(department);
            }

            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();

            return JsonResult.me().setMsg("操作失败");
        }
    }

    /**
     * 分页
     * @param query
     * @return
     */
    @PostMapping
    @ApiOperation(value = "分页查询数据" )
    @PreAuthorize(name="部门分页",sn = "department:list")
    public PageList<Department> querPage(@RequestBody DepartmentQuery query){

        return service.queryPage(query);

    }




}
